<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Common\Utils\JsonResponse;
use App\Http\Request\System\Dic\ListRequest;
use App\Http\Request\System\Dic\AddRequest;
use App\Http\Request\System\Dic\EditRequest;
use App\Http\Request\System\Dic\DeleteRequest;
use App\Http\Request\System\Dic\ListitemRequest;
use App\Http\Request\System\Dic\AllitemRequest;
use App\Http\Request\System\Dic\AdditemRequest;
use App\Http\Request\System\Dic\EdititemRequest;
use App\Http\Request\System\Dic\DeleteitemRequest;
use Illuminate\Http\Request;

class DicController extends Controller
{
    /**
     * @var \App\Services\System\DicService $dicService
     */
    private $dicService;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
        $this->dicService = app('dicService');
    }

    /**
     * 主页
     */
    public function index()
    {
        return view('systems.dics.index');
    }

    /**
     * 获取列表
     * @param ListRequest $request
     * @return array
     */
    public function getList(ListRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->dicService->getList($requestData);
                return JsonResponse::get($result['data'],TRUE,$result['total'],!empty($requestData['limit'])?$requestData['limit']:10);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 新增
     * @param AddRequest $request
     * @return array
     */
    public function add(AddRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                
                $result = $this->dicService->add($requestData);
                 /** @var \App\Services\System\LogService $logService */
                $logService = app('logService');
                $logService->log('39','添加数据定义', "添加数据定义，字典编码[{$requestData['diccode']}]字典名称[{$requestData['dicname']}]");
                return JsonResponse::success($result);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 编辑
     * @param EditRequest $request
     * @return array
     */
    public function edit(EditRequest $request)
    {
        DB::beginTransaction();
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->dicService->edit($requestData);
                DB::commit();
                $logService = app('logService');
                $logService->log('42','修改数据定义', "修改数据定义，编号[{$requestData['id']}]字典编码[{$requestData['diccode']}]字典名称[{$requestData['dicname']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            DB::rollBack();
            return $this->error($e);
        }
    }

    /**
     * 删除
     * @param DeleteRequest $request
     * @return array
     */
    public function delete(DeleteRequest $request)
    {
        DB::beginTransaction();
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->dicService->delete($requestData);
                DB::commit();
                $logService = app('logService');
                $logService->log('43','删除数据定义', "删除数据定义，编号[{$requestData['id']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            DB::rollBack();
            return $this->error($e);
        }
    }


    /**
     * 获取条目列表
     * @param ListitemRequest $request
     * @return array
     */
    public function getItemList(ListitemRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->dicService->getItemList($requestData);
                return JsonResponse::get($result['data'],TRUE,$result['total']);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 获取字典所有条目
     * @param AllitemRequest $request
     * @return array
     */
    public function getAllItems(AllitemRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->dicService->getAllItems($requestData);
                return JsonResponse::get($result);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 新增条目
     * @param AdditemRequest $request
     * @return array
     */
    public function addItem(AdditemRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->dicService->addDicItem($requestData);       
                $logService = app('logService');
                $logService->log('167','新增数据定义项', "新增数据定义项，字典编码[{$requestData['diccode']}]定义项编码[{$requestData['itemcode']}]定义项名称[{$requestData['itemname']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 编辑条目
     * @param EdititemRequest $request
     * @return array
     */
    public function editItem(EdititemRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->dicService->editDicItem($requestData);
                $logService = app('logService');
                $logService->log('168','修改数据定义项', "修改数据定义项，编号[{$requestData['id']}]字典编码[{$requestData['diccode']}]定义项编码[{$requestData['itemcode']}]定义项名称[{$requestData['itemname']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 删除条目
     * @param DeleteitemRequest $request
     * @return array
     */
    public function deleteItem(DeleteitemRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->dicService->deleteDicItem($requestData);
                $logService = app('logService');
                $logService->log('169','删除数据定义项', "删除数据定义项，编号[{$requestData['id']}]");
                return JsonResponse::operate();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
    * @Description: //获取字典列表通过code ---------- 公用接口
    * @Author: kzj
    * @datetime: 2021-10-22
    */
    public function getDicListByCode(Request $request)
    {
        try
        {
            $result = $this->dicService->getAllItems($request->all());
            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
    * @Description: //获取字典列表通过code(Array) ---------- 公用接口
    * @Author: kzj
    * @datetime: 2021-10-22
    */
    public function getDicListByCodes(Request $request)
    {
        try
        {
            $result = $this->dicService->getAllItemsByCodes($request->all());
            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }
}
